<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="规则标题" prop="ruleTitle">
        <el-input
          v-model="queryParams.ruleTitle"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker
          v-model="dateValue"
          type="datetimerange"
          range-separator="-"
          start-placeholder="起始时间"
          end-placeholder="终止"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="setQueryDate"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:rule:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ruleList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="规则标题" align="center" prop="ruleTitle" />
      <el-table-column label="所属大类" align="center" prop="ruleClass" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rule_class" :value="scope.row.ruleClass"/>
        </template>
      </el-table-column>
      <el-table-column label="所属类型" align="center" prop="ruleTypeName" />
      <el-table-column label="语言版本" align="center" prop="ruleLang">
        <template slot-scope="scope">
          {{scope.row.ruleLang=="zh-CN"?"中文":"英文"}}
        </template>
      </el-table-column>
      <el-table-column label="权威解读1标题" align="center" prop="explainTitle1" />
      <el-table-column label="是否首推" align="center" prop="isFirst" >
        <template slot-scope="scope">
          {{scope.row.isFirst=="0"?"否":"是"}}
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="pubUserName" />
      <el-table-column label="发布时间" align="center" prop="pubTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pubTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:rule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:rule:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改技术规则对话框 -->
    <el-dialog v-if="open" :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="发布的语言版本" prop="ruleLang">
          <el-radio-group v-model="form.ruleLang">
            <el-radio label="zh-CN">中文</el-radio>
            <el-radio label="en">英文</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否首推" prop="isFirst">
          <el-radio-group v-model="form.isFirst">
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属大类" prop="ruleClass">
          <el-select v-model="form.ruleClass" placeholder="请选所属大类">
            <el-option
              v-for="dict in dict.type.rule_class"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属类型" prop="ruleType">
          <el-select v-model="form.ruleType" multiple placeholder="请选所属类型">
            <el-option-group
              v-for="group in ruleTypeOptions"
              :key="group.label"
              :label="group.label">
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="发布人员" prop="pubUser">
          <el-select v-model="form.pubUser" placeholder="请输入发布人" clearable>
            <el-option v-for="item in userList" :key="item.userId" :label="item.nickName"
                       :value="item.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="发布时间" prop="pubTime">
          <el-date-picker
            v-model="form.pubTime"
            type="datetime"
            placeholder="选择时间"
            value-format="timestamp">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="规则标题" prop="ruleTitle">
          <el-input v-model="form.ruleTitle" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="规则内容" prop="ruleContent">
          <wangEditor  v-model="form.ruleContent" :min-height="192" key="123"/>
        </el-form-item>
        <el-form-item label="缩略图" prop="ruleImage">
          <image-upload v-model="form.ruleImage" :suggestSize="'720*434'" :limit="1"/>
        </el-form-item>
        <el-form-item label="权威解读1标题" prop="explainTitle1">
          <el-input v-model="form.explainTitle1" placeholder="请输入权威解1读标题" />
        </el-form-item>
        <el-form-item label="权威解读1时间" prop="explainTime1">
          <el-date-picker
            v-model="form.explainTime1"
            type="datetime"
            placeholder="选择时间"
            value-format="timestamp">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="权威解读1内容" prop="explainContent1">
          <wangEditor v-model="form.explainContent1" :min-height="240" />
        </el-form-item>
        <el-form-item label="权威解读1缩略图" prop="explainImage1">
          <image-upload v-model="form.explainImage1" :suggestSize="'720*434'" :limit="1"/>
        </el-form-item>
        <el-form-item label="权威解读2标题" prop="explainTitle2">
          <el-input v-model="form.explainTitle2" placeholder="请输入权威解读2标题" />
        </el-form-item>
        <el-form-item label="权威解读2时间" prop="explainTime2">
          <el-date-picker
            v-model="form.explainTime2"
            type="datetime"
            placeholder="选择时间"
            value-format="timestamp">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="权威解读2内容" prop="explainContent2">
          <wangEditor v-model="form.explainContent2" :min-height="240" />
        </el-form-item>
        <el-form-item label="权威解读2缩略图" prop="explainImage2">
          <image-upload v-model="form.explainImage2" :suggestSize="'720*434'" :limit="1"/>
        </el-form-item>
        <el-form-item label="权威解读3标题" prop="explainTitle3">
          <el-input v-model="form.explainTitle3" placeholder="请输入权威解读3标题" />
        </el-form-item>
        <el-form-item label="权威解读3时间" prop="explainTime3">
          <el-date-picker
            v-model="form.explainTime3"
            type="datetime"
            placeholder="选择时间"
            value-format="timestamp">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="权威解读3内容" prop="explainContent3">
          <wangEditor v-model="form.explainContent3" :min-height="240" />
        </el-form-item>
        <el-form-item label="权威解读3缩略图" prop="explainImage3">
          <image-upload v-model="form.explainImage3" :suggestSize="'720*434'" :limit="1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRule, getRule, delRule, addRule, updateRule,getRuleTypes } from "@/api/system/rule";
import { getSelectUser} from "@/api/system/user";

export default {
  name: "Rule",
  dicts: ['rule_class'],
  data() {
    return {
      userList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 技术规则表格数据
      ruleList: [],
      ruleTypeOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ruleTitle: null,
        ruleLang: null,
        isFirst: null,
        ruleClass: null,
        ruleType: null,
        pubUser: null,
        pubTime: null,
        ruleContent: null,
        ruleImage: null,
        explainTitle1: null,
        explainContent1: null,
        explainImage1: null,
        explainTitle2: null,
        explainContent2: null,
        explainImage2: null,
        explainTitle3: null,
        explainContent3: null,
        explainImage3: null,
      },
      dateValue:[],
      // 表单参数
      form: {
        ruleLang: "zh-CN",
        isFirst:"0",
      },
      // 表单校验
      rules: {
        ruleLang: [
          { required: true, message: "请选择语言版本", trigger: "blur" },
        ],
        isFirst: [
          { required: true, message: "请选择是否首推", trigger: "blur" },
        ],
        ruleClass: [
          { required: true, message: "请选择所属大类", trigger: "blur" },
        ],
        ruleType: [
          { required: true, message: "请选择所属类型", trigger: "blur" },
        ],
        pubUser: [
          { required: true, message: "请选择发布人员", trigger: "blur" },
        ],
        pubTime: [
          { required: true, message: "请选择发布时间", trigger: "blur" },
        ],
        ruleTitle: [
          { required: true, message: "请填写规则标题", trigger: "blur" },
          {max: 200, message: '规则标题长度不能超过200', trigger: 'blur' }
        ],
        ruleContent: [
          { required: true, message: "请填写规则内容", trigger: "blur" },
        ],
        ruleImage: [
          { required: true, message: "请上传缩略图", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getRuleTypes();
    this.getUserList();
  },
  methods: {
    getUserList(){
      getSelectUser().then((res) => {
        this.userList = res.data
      })
    },
    setQueryDate(value){
      console.info(new Date(value[1].get));
      this.queryParams.pubEndTime=new Date(value[1]).getTime()
      this.queryParams.pubStartTime=new Date(value[0]).getTime()
    },
    /** 查询技术规则列表 */
    getList() {
      this.loading = true;
      listRule(this.queryParams).then(response => {
        this.ruleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getRuleTypes() {
      getRuleTypes().then(response => {
        response.data.forEach(model=>{
          let option = {};
          option.label = model.key;
          let childArr = [];
          model.value.forEach(y =>{
              let child = {};
              child.label = y.typeNameCn;
              child.value = y.id;
              childArr.push(child);
          })
          option.options = childArr;
          this.ruleTypeOptions.push(option)
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ruleTitle: null,
        ruleLang: "zh-CN",
        isFirst: "0",
        ruleClass: null,
        ruleType: null,
        pubUser: null,
        pubTime: null,
        ruleContent: null,
        ruleImage: null,
        explainTitle1: null,
        explainContent1: null,
        explainImage1: null,
        explainTitle2: null,
        explainContent2: null,
        explainImage2: null,
        explainTitle3: null,
        explainContent3: null,
        explainImage3: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.pubStartTime=''
      this.queryParams.pubEndTime=''
      this.dateValue=[]
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加技术规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.title = "修改技术规则";
      this.open = true;
      // this.$nextTick(()=>{
        setTimeout(()=>{
           getRule(id).then(response => {
          this.form = response.data;
             if (this.form.ruleType){
               this.form.ruleType = this.form.ruleType.split(",").map(x=>parseInt(x));
             }
        });
        },100)

      // })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          if (this.form.ruleType){
            this.form.ruleType = this.form.ruleType.join(",");
          }
          if (this.form.id != null) {
            updateRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delRule(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/rule/export', {
        ...this.queryParams
      }, `rule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
